package com.mall.controller;

import com.mall.common.R;
import com.mall.entity.SmoothMallCart;
import com.mall.entity.SmoothMallOrder;
import com.mall.entity.dto.SmoothMallOrderDto;
import com.mall.service.SmoothMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

@RestController
public class SmoothMallOrderController {
    @Autowired
    SmoothMallOrderService smoothMallOrderService;
    @PostMapping("/order/create")
    public R createOrder(@RequestBody SmoothMallOrder smoothMallOrder){
        boolean order = smoothMallOrderService.createOrder(smoothMallOrder);
        if (order){
            return R.success("创建订单成功！");
        }
        return R.error("创建订单失败！");
    }
    @GetMapping("/order/list")
    public List<SmoothMallOrderDto> getOrderDto(){
        return smoothMallOrderService.getDtoList();
    }

    @PreAuthorize("@maxAnger.hasAuthority('admin')")
    @GetMapping("/admin/order/list")
    public List<SmoothMallOrderDto> getAdminOrderDto(){
        return smoothMallOrderService.getAdminDtoList();
    }
    @PostMapping("/order/cancel/{orderNo}")
    public R deleteOrder(@PathVariable String orderNo){
        boolean b = smoothMallOrderService.deleteOrderForOrderNo(orderNo);
        if (b){
            return R.success("取消订单成功！");
        }else {
            return R.error("取消订单失败！");
        }
    }
    @PostMapping("/admin/order/delivered/{orderNo}")
    public R orderDelivered(@PathVariable String orderNo){
        boolean b = smoothMallOrderService.orderDelivered(orderNo);
        if (b){
            return R.success("发货成功！");
        }
        return R.error("发货失败！");
    }
    @PostMapping("/admin/order/finished/{orderNo}")
    public R orderFinish(@PathVariable String orderNo){
        boolean b = smoothMallOrderService.orderFinished(orderNo);
        if (b){
            return R.success("完结成功！");
        }
        return R.error("完结失败！");
    }
}
